package com.epichust.bootumpp.service.impl;


import com.epichust.bootumpp.model.dao.Mbf5ProduceOperationDao;
import com.epichust.bootumpp.model.entity.Mbf5ProduceOpEresource;
import com.epichust.bootumpp.model.entity.Mbf5ProduceOperation;
import com.epichust.bootumpp.service.IMbf5ProduceOperationService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class Mbf5ProduceOperationServiceImpl implements IMbf5ProduceOperationService {

    @Autowired
    private Mbf5ProduceOperationDao mbf5ProduceOperationDao;

    @Override
    public List<Mbf5ProduceOperation> getAllByParams(Map<String, String> map) {
        List<Mbf5ProduceOperation> list = mbf5ProduceOperationDao.getAllByParams(map);
        return list;
    }

    @Override
    public void saveOrUpdate(Map<String, String> map) {
        Gson gson = new GsonBuilder().serializeNulls().create();
        String mbfDefOperationFlag = map.get("mbfDefOperationFlag");
        map.remove("mbfDefOperationFlag");
        Mbf5ProduceOperation produceOperation = gson.fromJson(gson.toJson(map), Mbf5ProduceOperation.class);
        //新增
        if("add".equals(mbfDefOperationFlag)){
            produceOperation.setCreateDate(new Date());
            produceOperation.setModifyDate(new Date());
            mbf5ProduceOperationDao.insertProduceOperation(produceOperation);
        }else{//修改
            produceOperation.setModifyDate(new Date());
            mbf5ProduceOperationDao.updateProduceOperation(produceOperation);
        }
    }

    @Override
    public String common(Map<String, String> map) {
        String id = map.get("id");
        //del,active,freeze
        String flag = map.get("flag");
        mbf5ProduceOperationDao.common(map);
        if("del".equals(flag)){
            return "删除成功";
        }else if("active".equals(flag)){
            return "激活成功";
        }else if("freeze".equals(flag)){
            return "冻结成功";
        }else{
            return null;
        }
    }

    @Override
    public List<Map> getOpEresourceByParams(Map<String, String> map) {
        List<Map> list = mbf5ProduceOperationDao.getOpEresourceByParams(map);
        return list;
    }

    @Override
    public void saveOpEresource(Map<String, String> map) {
        Gson gson = new GsonBuilder().serializeNulls().create();
        String user = map.get("user");
        String opGid = map.get("opGid");
        String wcIds = map.get("wcIds");
        //先删除关联的工位资源
        mbf5ProduceOperationDao.delOpEresource(opGid);
        //再新增关联的工位资源
        List<String> wcIdList = gson.fromJson(wcIds, List.class);
        if(wcIdList.size() > 0){
            List<Mbf5ProduceOpEresource> dataList = new ArrayList<Mbf5ProduceOpEresource>();
            for(int i=0; i< wcIdList.size(); i++){
                Mbf5ProduceOpEresource opEresource = new Mbf5ProduceOpEresource();
                //查询相关gid
                Map<String, String> param = new HashMap<>();
                param.put("wcId",wcIdList.get(i));
                List<Map<String, String>> mapList = mbf5ProduceOperationDao.getGidByWcId(param);
                if(mapList.size() > 0){
                    Map<String, String> gidMap = mapList.get(0);
                    String workCenterGid = gidMap.get("WORKCENTERGID");// 工作中心主键
                    String workCellClassGid = gidMap.get("WORKCELLCLASSGID");//工位类别主键
                    opEresource.setWorkCenterGid(workCenterGid);
                    opEresource.setWorkCellClassGid(workCellClassGid);
                }
                opEresource.setWorkCellGid(wcIdList.get(i));
                opEresource.setOpGid(opGid);
                opEresource.setCreateId(user);
                opEresource.setModifyId(user);
                opEresource.setCreateDate(new Date());
                opEresource.setModifyDate(new Date());
                dataList.add(opEresource);
            }
            mbf5ProduceOperationDao.saveOpEresourceList(dataList);
        }
    }

    @Override
    public List<Map> getOpMresourceByParams(Map<String, String> map) {
        return null;
    }
    @Override
    public void saveOpMresource(Map<String, String> map) {

    }


}
